import {
  intoList,
  getInto,
  intoAdd,
  intoEdit,
  getSeo,
  intoSeoEdit,
  intoUpdate,
  intoSeqNum
} from '@/api/into'
import utils from '@/utils/common'
var timeStamp = Number(new Date())
const into = {
  state: {},

  mutations: {},

  actions: {
    // 应用详情
    GetInto ({
      getters
    }, data) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: data.id
      }
      return new Promise((resolve, reject) => {
        getInto(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 应用列表
    IntoList ({
      getters
    }, data) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        keyword: data.keyword,
        appTypeId: data.appTypeId,
        status: data.status,
        startTime: data.startTime,
        endTime: data.endTime,
        page: data.page || '',
        pageSize: data.pageSize || ''
      }
      return new Promise((resolve) => {
        intoList(loginParams).then((response) => {
          resolve(response)
        }).catch((error) => {
          resolve(error)
        })
      })
    },
    // 应用添加
    IntoAdd ({
      getters
    }, values) {
      var arr = []
      for (var v in values.appPics) {
        arr.push(values.appPics[v].url)
      }
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        packageName: values.packageName,
        name: values.name,
        appTypeId: values.appTypeId,
        version: values.version,
        developerName: values.developerName,
        copyrightPic: values.copyrightPic,
        qualificationDoc: values.qualificationDoc,
        packageUrl: values.packageUrl,
        scope: values.scope,
        tag: JSON.stringify(values.tag.split(',')),
        introduction: values.introduction,
        appPics: JSON.stringify(arr),
        iconUrl: values.iconUrl
      }
      return new Promise((resolve, reject) => {
        intoAdd(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 应用编辑
    IntoEdit ({
      getters
    }, values) {
      var arr = []
      for (var v in values.appPics) {
        arr.push(values.appPics[v].url)
      }
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: values.id,
        packageName: values.packageName,
        name: values.name,
        appTypeId: values.appTypeId,
        version: values.version,
        developerName: values.developerName,
        copyrightPic: values.copyrightPic,
        qualificationDoc: values.qualificationDoc,
        packageUrl: values.packageUrl,
        scope: values.scope,
        tag: JSON.stringify(values.tag.split(',')),
        introduction: values.introduction,
        appPics: JSON.stringify(arr),
        iconUrl: values.iconUrl
      }
      return new Promise((resolve, reject) => {
        intoEdit(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // seo详情
    GetSeo ({
      getters
    }, id) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: id
      }
      return new Promise((resolve, reject) => {
        getSeo(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 应用seo编辑
    IntoSeoEdit ({
      getters
    }, values) {
      var arr = []
      for (var v in values.appPics) {
        arr.push(values.appPics[v].url)
      }
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: values.id,
        seoTitle: values.Title,
        seoKeyword: JSON.stringify(values.Keywords.split(',')),
        seoDescirption: values.Description
      }
      return new Promise((resolve, reject) => {
        intoSeoEdit(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 应用上下架
    IntoUpdate ({
      getters
    }, values) {
      var arr = []
      for (var v in values.appPics) {
        arr.push(values.appPics[v].url)
      }
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: values.id,
        status: values.status
      }
      return new Promise((resolve, reject) => {
        intoUpdate(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 应用排序
    IntoSeqNum ({
      getters
    }, values) {
      const loginParams = {
        token: utils.AuthToken(timeStamp, getters.userKey),
        adminId: getters.adminId,
        timestamp: timeStamp,
        id: values.id,
        seqNum: values.seqNum
      }
      return new Promise((resolve, reject) => {
        intoSeqNum(loginParams).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default into
